import { Injectable } from '@angular/core';
import { BehaviorSubject, Subject } from 'rxjs';
import { ButtonGroupConfig, ButtonConfig, EditState } from '../core';

@Injectable()
export class FormBottonsConfigService {
  public buttonsConfig: ButtonGroupConfig[];

  state$ = new BehaviorSubject<EditState>(EditState.Empty);

  //public editState: EditState = EditState.Empty;
  public get onStateChanged() {
    return this.state$.asObservable();
  }

  public get editState() {
    return this.state$.getValue();
  }

  private save$ = new Subject();
  public saveButton: ButtonConfig = {
    name: '保存',
    fn: () => this.save$.next(),
    visable: true,
    setVisable: () => (this.saveButton.visable = true),
    enabled: true,
    setEnabled: () => {
      if (this.editState == EditState.Creating || this.editState == EditState.Editing) {
        this.saveButton.enabled = true;
      } else {
        this.saveButton.enabled = false;
      }
    },
  };
  public get onSaveClick() {
    return this.save$.asObservable();
  }

  private giveup$ = new Subject();
  public giveupButton: ButtonConfig = {
    name: '放弃',
    fn: () => this.giveup$.next(),
    visable: true,
    setVisable: () => (this.giveupButton.visable = true),
    enabled: true,
    setEnabled: () => {
      if (this.editState == EditState.Creating || this.editState == EditState.Editing) {
        this.giveupButton.enabled = true;
      } else {
        this.giveupButton.enabled = false;
      }
    },
  };
  public get onGiveupClick() {
    return this.giveup$.asObservable();
  }

  private new$ = new Subject();
  public newButton: ButtonConfig = {
    name: '新增',
    fn: () => this.new$.next(),
    visable: true,
    setVisable: () => (this.newButton.visable = true),
    enabled: true,
    setEnabled: () => (this.newButton.enabled = true),
  };
  public get onNewClick() {
    return this.new$.asObservable();
  }

  private copy$ = new Subject();
  public copyButton: ButtonConfig = {
    name: '复制',
    fn: () => this.copy$.next(),
    visable: true,
    setVisable: () => (this.copyButton.visable = true),
    enabled: true,
    setEnabled: () => {
      if (
        this.editState == EditState.View ||
        this.editState == EditState.Editing ||
        this.editState == EditState.Appoving ||
        this.editState == EditState.Appoved
      ) {
        this.copyButton.enabled = true;
      } else {
        this.copyButton.enabled = false;
      }
    },
  };
  public get onCopyClick() {
    return this.copy$.asObservable();
  }

  private delete$ = new Subject();
  public deleteButton: ButtonConfig = {
    name: '删除',
    fn: () => this.delete$.next(),
    visable: true,
    setVisable: () => (this.deleteButton.visable = true),
    enabled: true,
    setEnabled: () => {
      if (
        this.editState == EditState.View ||
        this.editState == EditState.Editing ||
        this.editState == EditState.Appoving
      ) {
        this.deleteButton.enabled = true;
      } else {
        this.deleteButton.enabled = false;
      }
    },
  };
  public get onDeleteClick() {
    return this.delete$.asObservable();
  }

  private submit$ = new Subject();
  public submitButton: ButtonConfig = {
    name: '提交',
    fn: () => this.submit$.next(),
    visable: true,
    setVisable: () => (this.submitButton.visable = true),
    enabled: true,
    setEnabled: () => {
      if (this.editState == EditState.View) {
        this.submitButton.enabled = true;
      } else {
        this.submitButton.enabled = false;
      }
    },
  };
  public get onSubmitClick() {
    return this.submit$.asObservable();
  }

  private back$ = new Subject();
  public backButton: ButtonConfig = {
    name: '撤销',
    fn: () => this.back$.next(),
    visable: true,
    setVisable: () => (this.backButton.visable = true),
    enabled: true,
    setEnabled: () => {
      if (this.editState == EditState.Appoving) {
        this.backButton.enabled = true;
      } else {
        this.backButton.enabled = false;
      }
    },
  };
  public get onBackClick() {
    return this.back$.asObservable();
  }

  private approve$ = new Subject();
  public approveButton: ButtonConfig = {
    name: '审核',
    fn: () => this.approve$.next(),
    visable: true,
    setVisable: () => (this.approveButton.visable = true),
    enabled: true,
    setEnabled: () => {
      if (this.editState == EditState.Appoving) {
        this.approveButton.enabled = true;
      } else {
        this.approveButton.enabled = false;
      }
    },
  };
  public get onApproveClick() {
    return this.approve$.asObservable();
  }

  private unapprove$ = new Subject();
  public unapproveButton: ButtonConfig = {
    name: '弃审',
    fn: () => this.unapprove$.next(),
    visable: true,
    setVisable: () => (this.unapproveButton.visable = true),
    enabled: true,
    setEnabled: () => {
      if (this.editState == EditState.Appoved) {
        this.unapproveButton.enabled = true;
      } else {
        this.unapproveButton.enabled = false;
      }
    },
  };
  public get onUnapproveClick() {
    return this.unapprove$.asObservable();
  }

  private find$ = new Subject();
  public findButton: ButtonConfig = {
    name: '查找',
    fn: () => this.find$.next(),
    visable: true,
    setVisable: () => (this.findButton.visable = true),
    enabled: true,
    setEnabled: () => (this.findButton.enabled = true),
  };
  public get onFindClick() {
    return this.find$.asObservable();
  }

  private list$ = new Subject();
  public listButton: ButtonConfig = {
    name: '列表',
    fn: () => this.list$.next(),
    visable: true,
    setVisable: () => (this.listButton.visable = true),
    enabled: true,
    setEnabled: () => (this.listButton.enabled = true),
  };
  public get onListClick() {
    return this.list$.asObservable();
  }

  private first$ = new Subject();
  public firstButton: ButtonConfig = {
    name: '首页',
    fn: () => this.first$.next(),
    visable: true,
    setVisable: () => (this.firstButton.visable = true),
    enabled: true,
    setEnabled: () => (this.firstButton.enabled = true),
  };
  public get onFirstClick() {
    return this.first$.asObservable();
  }

  private pre$ = new Subject();
  public preButton: ButtonConfig = {
    name: '上一页',
    fn: () => this.pre$.next(),
    visable: true,
    setVisable: () => (this.preButton.visable = true),
    enabled: true,
    setEnabled: () => (this.preButton.enabled = true),
  };
  public get onPreClick() {
    return this.pre$.asObservable();
  }

  private next$ = new Subject();
  public nextButton: ButtonConfig = {
    name: '下一页',
    fn: () => this.next$.next(),
    visable: true,
    setVisable: () => (this.nextButton.visable = true),
    enabled: true,
    setEnabled: () => (this.preButton.enabled = true),
  };
  public get onNextClick() {
    return this.next$.asObservable();
  }

  private last$ = new Subject();
  public lastButton: ButtonConfig = {
    name: '尾页',
    fn: () => this.last$.next(),
    visable: true,
    setVisable: () => (this.lastButton.visable = true),
    enabled: true,
    setEnabled: () => (this.preButton.enabled = true),
  };
  public get onLastClick() {
    return this.last$.asObservable();
  }

  private export$ = new Subject();
  public exportButton: ButtonConfig = {
    name: '导出',
    fn: () => this.export$.next(),
    visable: true,
    setVisable: () => (this.exportButton.visable = true),
    enabled: true,
    setEnabled: () => {
      if (this.editState == EditState.Appoved) {
        this.exportButton.enabled = true;
      } else {
        this.exportButton.enabled = false;
      }
    },
  };
  public get onExportClick() {
    return this.export$.asObservable();
  }

  private print$ = new Subject();
  public printButton: ButtonConfig = {
    name: '打印',
    fn: () => this.print$.next(),
    visable: true,
    setVisable: () => (this.printButton.visable = true),
    enabled: true,
    setEnabled: () => {
      if (this.editState == EditState.Appoved) {
        this.printButton.enabled = true;
      } else {
        this.printButton.enabled = false;
      }
    },
  };
  public get onPrintClick() {
    return this.print$.asObservable();
  }

  public operateButtonGroup: ButtonGroupConfig;
  public approveButtonGroup: ButtonGroupConfig;
  public searchButtonGroup: ButtonGroupConfig;
  public pageButtonGroup: ButtonGroupConfig;
  public outputButtonGroup: ButtonGroupConfig;

  constructor() {
    this.operateButtonGroup = {
      buttonGroup: [
        this.saveButton,
        this.giveupButton,
        this.newButton,
        this.copyButton,
        this.deleteButton,
      ],
      visable: true,
    };

    this.approveButtonGroup = {
      buttonGroup: [this.submitButton, this.backButton, this.approveButton, this.unapproveButton],
      visable: true,
    };

    this.searchButtonGroup = {
      buttonGroup: [this.findButton, this.listButton],
      visable: true,
    };

    this.pageButtonGroup = {
      buttonGroup: [this.firstButton, this.preButton, this.nextButton, this.lastButton],
      visable: false,
    };

    this.outputButtonGroup = {
      buttonGroup: [this.exportButton, this.printButton],
      visable: false,
    };

    this.buttonsConfig = [
      this.operateButtonGroup,
      this.approveButtonGroup,
      this.searchButtonGroup,
      this.pageButtonGroup,
      this.outputButtonGroup,
    ];

    this.changeState(EditState.Empty);
  }

  public changeState(state: EditState) {
    this.state$.next(state);

    for (var groupConfig of this.buttonsConfig) {
      for (var buttonConfig of groupConfig.buttonGroup) {
        if (buttonConfig.setVisable) buttonConfig.setVisable();
        if (buttonConfig.setEnabled) buttonConfig.setEnabled();
      }
    }
  }
}
